package com.whz.mall.produce.exception;

import com.whz.common.utils.Query;
import com.whz.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * Create by Z on 2021/2/10.
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvise {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R handleValidException(MethodArgumentNotValidException e){
        Map<String, String> result = new HashMap<>();
        e.getBindingResult().getFieldErrors().stream().forEach(error -> {
            String field = error.getField();
            String defaultMessage = error.getDefaultMessage();
            result.put(field,  defaultMessage);
        });

        return R.error(-1, "aaa").put("data", result);
    }
}
